package com.it.web.action;

import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.util.HashMap;
import java.util.Map;

import javax.ws.rs.core.MediaType;

import org.apache.commons.io.FileUtils;
import org.apache.cxf.jaxrs.client.WebClient;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionContext;

import cn.itcast.bos.domain.delivery.Constants;
import cn.itcast.bos.domain.delivery.Promotion;
import cn.itcast.bos.domain.pageBean.PageBean;
import freemarker.template.Configuration;
import freemarker.template.Template;

@ParentPackage("json-default")
@Namespace("/")
@Scope("prototype")
@SuppressWarnings("all")
@Controller
public class PromotionAction extends BaseAction<Promotion>{
	private static final long serialVersionUID = 1L;

	@Action(value="promotion_show", results={@Result(name="success", type="json")})
	public String pageQuery() {//http://localhost:9001/bos_management/services/promotionservice/pageShow?page=2&rows=1
		PageBean<Promotion> pageBean = WebClient.create("http://localhost:9001/bos_management/"
				+ "services/promotionservice/pageShow?page="+ page + "&rows=" + rows)
				.accept(MediaType.APPLICATION_JSON).get(PageBean.class);
		
		ActionContext.getContext().getValueStack().push(pageBean);
	
		return SUCCESS;
	}
	
	@Action(value="promotion_detail")
	public String showDetail() throws Exception {
		String htmlRealPath = ServletActionContext.getServletContext().getRealPath("/freemaker/");
		File detailFile = new File(htmlRealPath + "/" + model.getId() + ".html"); 
		
		//如果存在HTML不存在，加载数据库
		if (!detailFile.exists()) {
			Configuration cfg = new Configuration(Configuration.VERSION_2_3_22);
			cfg.setDirectoryForTemplateLoading(new File(ServletActionContext
					.getServletContext().getRealPath("/WEB-INF/freemaker_templates")));
			//获取模板对象
			Template tp = cfg.getTemplate("promotion-detail.ftl");
			//动态数据
			Promotion promotion = WebClient.create("http://localhost:9001/bos_management/services/promotionservice/promotion/" + 
					model.getId()).accept(MediaType.APPLICATION_JSON).get(Promotion.class);
			Map<String, Object> parameterMap = new HashMap<>();
			parameterMap.put("promotion", promotion);
			//合并输出
			tp.process(parameterMap, new OutputStreamWriter(new FileOutputStream(detailFile), "UTF-8"));
		}
		ServletActionContext.getResponse().setContentType("text/html; charset=UTF-8");
		FileUtils.copyFile(detailFile, ServletActionContext.getResponse().getOutputStream());
		
		return NONE;
	}
	@Action(value = "promotion_showDetail")
	public String showDetail2() throws Exception {
		// 先判断 id 对应html 是否存在，如果存在 直接返回
		String htmlRealPath = ServletActionContext.getServletContext()
				.getRealPath("/freemaker");
		File htmlFile = new File(htmlRealPath + "/" + model.getId() + ".html");

		// 如果html文件不存在 ，查询数据库，结合freemarker模板生成 页面
		if (!htmlFile.exists()) {
			Configuration configuration = new Configuration(
					Configuration.VERSION_2_3_22);
			configuration.setDirectoryForTemplateLoading(new File(
					ServletActionContext.getServletContext().getRealPath(
							"/WEB-INF/freemaker_templates")));
			// 获取模板对象
			Template template = configuration
					.getTemplate("promotion-detail.ftl");

			// 动态数据
			Promotion promotion = WebClient
					.create(Constants.BOS_MANAGEMENT_URL
							+ "/bos_management/services/promotionservice/promotion/"
							+ model.getId()).accept(MediaType.APPLICATION_JSON)
					.get(Promotion.class);
			Map<String, Object> parameterMap = new HashMap<String, Object>();
			parameterMap.put("promotion", promotion);

			// 合并输出
			template.process(parameterMap, new OutputStreamWriter(
					new FileOutputStream(htmlFile), "utf-8"));

		}

		// 存在 ，直接将文件返回
		ServletActionContext.getResponse().setContentType(
				"text/html;charset=utf-8");
		FileUtils.copyFile(htmlFile, ServletActionContext.getResponse()
				.getOutputStream());

		return NONE;
	}
}
